package com.example.utils;

import com.example.entity.Article;
import com.example.vo.HotArticleVo;
import lombok.val;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

public class BeanCopyUtils {

    private BeanCopyUtils(){

    }

    public static <V> V copyBean(Object source, Class<V> clazz) {
        V result =null;
        try {
            result = clazz.newInstance();
            BeanUtils.copyProperties(source,result);
        } catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }

    public static <O,V> List<V> copyBeanList(List<O> list, Class <V> clazz){
        return list.stream()
                .map(o -> copyBean(o,clazz))
                .collect(Collectors.toList());
    }
//    public static void main(String[] args){
//        Article article = new Article();
//        article.setTitle("ss");
//        article.setId(1L);
//
//        HotArticleVo hotArticleVo = copyBean(article, HotArticleVo.class);
//        System.err.println(hotArticleVo);
//    }
}
